package hengtiansoft.com.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

import hengtiansoft.com.myapplication.ormlite.OrmListTest;

public class MainActivity extends BaseActivity {
    private long times;
    File soundFile;
    MediaRecorder mRecorder;
    MediaPlayer mPlayer = new MediaPlayer();
    TextView textView;
    View tt;
    RadioButton radioBut1;
    SharedPreferences sp;
    Button tolist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initData() {
    }

    @Override
    public void initView() {
        mRecorder = new MediaRecorder();
        tolist = (Button) findViewById(R.id.tolist);
        tolist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, MyListView.class);
                startActivity(i);
            }
        });
        radioBut1 = (RadioButton) findViewById(R.id.radioButton2);
        radioBut1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, OrmListTest.class);
                startActivity(i);
            }
        });
        tt = findViewById(R.id.i1);
        tt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, ScrollMyActivity.class);
                startActivity(i);
            }
        });

        Button play = (Button) findViewById(R.id.play);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    Toast.makeText(MainActivity.this, "SDCARD不存在", Toast.LENGTH_LONG).show();

                    return;
                }

                try {
                    soundFile = new File(Environment.getExternalStorageDirectory().getCanonicalFile() + "/sound.amr");
                    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置录音声音来源
                    //设置录制声音的输出格式(需要在设置声音编码格式之前设置)
                    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    //设置声音编码的格式
                    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    mRecorder.setOutputFile(soundFile.getAbsolutePath());
                    mRecorder.prepare();
                    mRecorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Button stop = (Button) findViewById(R.id.stop);
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (soundFile != null && soundFile.exists()) {
                    mRecorder.stop();
                    mRecorder.release();
                    mRecorder = null;
                }
            }
        });
        Button run = (Button) findViewById(R.id.run);
        run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    mPlayer.setDataSource("/mnt/sdcard/sound.amr");
                    mPlayer.prepare();
                    mPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        Button stopRun = (Button) findViewById(R.id.stopruning);
        stopRun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.stop();
            }
        });
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, UrlView.class);
                MainActivity.this.startActivity(i);
            }
        });
        textView = (TextView) findViewById(R.id.textView3);
        findViewById(R.id.more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, CatchPacture.class);
                startActivity(i);
            }
        });
        textView.setText("MODEL:" + Build.MODEL + "\n" + "MANUFACTURER:" + Build.MANUFACTURER);
        Log.e("luModel:", Build.MODEL);
        Log.e("luManfacture:", Build.MANUFACTURER);
        findViewById(R.id.bluetooth).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, Bluetoolth.class);
                startActivity(i);
            }
        });
    }

    @Override
    protected void onDestroy() {
        mPlayer.stop();
        if (soundFile != null && soundFile.exists() && mRecorder != null) {
            //停止
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
        super.onDestroy();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (System.currentTimeMillis() - times > 2000) {
                    times = System.currentTimeMillis();
                    Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_LONG).show();
                } else {
                    Log.e("lu", "exit");
//                    System.exit(0);
//                    android.os.Process.killProcess(android.os.Process.myPid());
                    this.finish();
                    /**
                     * 正确的做法应该是this.finish();
                     * 上面2种做法都是直接杀死主进程,(不走Activity的生命周期)
                     * 会把主进程上的服务全部都关闭
                     * 这边只要是蓝牙服务还在后台运行不能主动关闭
                     *
                     * 据说这2个方法如果是在第2个Activity中执行的,
                     * 系统会自动再启动App,经测试不会重启
                     */
                }
            }
        }
        return true;
    }
}
